package com.dhcc.sds.standard.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.sds.standard.dto.TreeNode;
import com.dhcc.sds.standard.entity.SdsStandDicMatchParamEntity;
import com.dhcc.sds.standard.service.ISdsStandDicMatchParamService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 项目标准化字典
 * 
 * @ClassName: SdsStandDicController
 * @Description: TODO
 * @author zhangdc
 * @date 2023-02-15 10:09:57
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstanddic")
public class SdsStandDicController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstanddic/";

	@Autowired
	private ISdsStandDicService sdsStandDicService;

    @Autowired
    private ISdsStandSourceService sdsStandSourceService;

    @Autowired
    private ISdsStandDicMatchParamService sdsStandDicMatchParamService;
	

    /**
     * 跳转到项目标准化字典列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "sdsstanddic.html";
    }
	
    /**
     * 跳转到新增项目标准化字典
     */
    @RequestMapping("/to_add/{parentId}")
    public String toAdd(@PathVariable Long parentId, Model model) {
        model.addAttribute("parentId", parentId);
        return PREFIX + "sdsstanddic_add.html";
    }
	
	/**
     * 跳转到修改项目标准化字典
     */
    @RequestMapping("/to_update/{sdsStandDicId}")
    public String toUpdate(@PathVariable Long sdsStandDicId, Model model) {
        SdsStandDicEntity sdsStandDic = this.sdsStandDicService.selectById(sdsStandDicId);
        SdsStandDicEntity parentDic = sdsStandDicService.selectById(sdsStandDic.getParentId());
        String parentName = "";
        if (!StringUtils.isEmpty(parentDic)){
            parentName = parentDic.getName();
        }
        model.addAttribute("sdsStandDic",sdsStandDic);
        model.addAttribute("parentName", parentName);
        return PREFIX + "sdsstanddic_edit.html";
    }

    /**
     * 跳转到标准化字典对照参数维护
     */
    @RequestMapping("/to_showDicMatch/{sdsStandDicId}")
    public String toShowDicMatch(@PathVariable Long sdsStandDicId, Model model) {
        model.addAttribute("dicId",sdsStandDicId);
        return PREFIX + "sdsstanddic_match.html";
    }

    @RequestMapping("/to_showDicSubMatch/{sdsStandDicSubId}")
    public String toShowDicSubMatch(@PathVariable Long sdsStandDicSubId, Model model) {
        model.addAttribute("dicId",0);
        model.addAttribute("dicsubId",sdsStandDicSubId);
        return PREFIX + "sdsstanddic_match.html";
    }

    /**
     * 获取项目标准化字典列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsStandDicEntity> page = new PageFactory<SdsStandDicEntity>().defaultPage();
        List<SdsStandDicEntity> result = this.sdsStandDicService.page(page,params);
        page.setRecords((List<SdsStandDicEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增项目标准化字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandDicEntity sdsStandDic) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsStandDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandDic.setName(java.net.URLDecoder.decode(sdsStandDic.getName(), "UTF-8"));
        sdsStandDic.init();
        sdsStandDic.insert();
        return R.ok();
    }

    /**
     * 删除项目标准化字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandDicId) {
	
        this.sdsStandDicService.deleteById(sdsStandDicId);

        return R.ok();
    }

    /**
     * 修改项目标准化字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandDicEntity sdsStandDic) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsStandDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        String name = java.net.URLDecoder.decode(sdsStandDic.getName(), "UTF-8");
        SdsStandDicEntity old = this.sdsStandDicService.selectById(sdsStandDic.getId());
        old.setParentId(sdsStandDic.getParentId());
        old.setName(name);
        old.setLevels(sdsStandDic.getLevels());
        old.setCompareType(sdsStandDic.getCompareType());
        old.setResume(sdsStandDic.getResume());
        old.init();
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 修改标准字典对照参数表
     */
    @RequestMapping(value = "/updateMatch")
    @ResponseBody
    public Object updateMatch(SdsStandDicMatchParamEntity sdsStandCompareParamEntity) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsStandCompareParamEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        String content = java.net.URLDecoder.decode(sdsStandCompareParamEntity.getCompareContent(), "UTF-8");

        SdsStandDicMatchParamEntity old = sdsStandDicMatchParamService.selectById(sdsStandCompareParamEntity.getId());

        old.setDicId(sdsStandCompareParamEntity.getDicId());
        old.setCompareType(sdsStandCompareParamEntity.getCompareType());
        old.setFilterType(sdsStandCompareParamEntity.getFilterType());
        old.setCompareContent(content);
        old.setResume(sdsStandCompareParamEntity.getResume());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 新增标准字典对照参数表
     */
    @RequestMapping(value = "/addMatch")
    @ResponseBody
    public Object addMatch(SdsStandDicMatchParamEntity sdsStandDicMatchParamEntity) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsStandDicMatchParamEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandDicMatchParamEntity.setCompareContent(java.net.URLDecoder.decode(sdsStandDicMatchParamEntity.getCompareContent(), "UTF-8"));
        sdsStandDicMatchParamEntity.insert();
        return R.ok();
    }

    /**
     * 删除标准字典对照参数表
     */
    @RequestMapping(value = "/deleteMatch")
    @ResponseBody
    public Object deleteMatch(@RequestParam Long sdsDicMatchId) {

        sdsStandDicMatchParamService.deleteById(sdsDicMatchId);

        return R.ok();
    }

    /**
     * 获取标准字典对照参数表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listMatch/{dicId}")
    @ResponseBody
    public Object listMatch(@RequestParam Map<String, Object> params, @PathVariable Long dicId) {
        params.put("dicId", dicId);
        Page<SdsStandDicMatchParamEntity> page = new PageFactory<SdsStandDicMatchParamEntity>().dataGridPage();
        List<SdsStandDicMatchParamEntity> result = sdsStandDicMatchParamService.page(page,params);
        page.setRecords((List<SdsStandDicMatchParamEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 跳转到导入项目标准字典对照参数页面
     */
    @RequestMapping("/to_import")
    public String toImport() {
        return PREFIX + "import.html";
    }

    /**
     * 导入项目标准字典对照参数
     */
    @RequestMapping(method = RequestMethod.POST, path = "/import")
    @ResponseBody
    public R importMatch(@RequestPart("file") MultipartFile file) {
        ExcelReader reader;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
            List<List<Object>> indexAll = reader.read();
            sdsStandDicMatchParamService.importMatch(indexAll);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 获取标准字典的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        return sdsStandDicService.tree();
    }


    /**
     *  下拉树
     */
    @RequestMapping("/getDicTree/{dataSourceId}")
    @ResponseBody
    public Object getDicTree(@PathVariable Long dataSourceId){
        /*Long parentId = 0l;
        List<TreeNode> dicTree = sdsStandDicService.getDicTree(parentId, dataSourceId);*/
        List<Tree<Long>> trees = sdsStandDicService.getDicTree2(dataSourceId);
        return trees;
    }

    @RequestMapping(value = "/isHaveChildren")
    @ResponseBody
    public Object isHaveChildren(@RequestParam Long sdsStandDicId) {

        boolean isHaveChildren = this.sdsStandDicService.isHaveChildren(sdsStandDicId);

        return isHaveChildren;
    }

    /**
     * 删除项目标准化字典及子节点
     */
    @RequestMapping(value = "/deleteAll")
    @ResponseBody
    public Object deleteAll(@RequestParam Long sdsStandDicId) {

        this.sdsStandDicService.deleteAllById(sdsStandDicId);

        return R.ok();
    }
	
}
